package server;

import classes.Game;
import java.io.*;
import java.net.*;
import networking.*;

public class Server {



    public Server() {
  //      games = new Calendar[200];
        running = true;
        liveGames = new Game[8];
        db = new DBconnector();
        try {
        db.initialize();
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * Boolean that denotes that server is running
     * Admin can kill server with admin.kill()
     */
    private boolean running;


    /**
     * Connection to database.
     */
    private DBconnector db;

    /**
     * Socket to connect to the server.
     */
    private ServerSocket tcp;

   /**
    * Socket to connect to a UDP server. Not used.
    */
    private DatagramSocket udp;
//
//    /*
//     * Games array where index contains the value of last update.
//     */
//    private Calendar[] games;

    /**
     * Live Games is an array of games that contains all the game information.
     * This is done for easy polling.
     *
     */
    private Game[] liveGames;

    /**
     * Admin is the admin user allowed to access the database and to modify
     * the live games.
     */
    private Admin admin;

    /**
     * @return the db
     */
    public DBconnector getDb() {
        return db;
    }

    /**
     * Getter for Live Games array
     */
    public Game[] getLiveGames() {
        return liveGames;
    }

    /**
     * Server kill function, exits while loop
     */
    public void kill() {
        running = false;
    }


//    /**
//     * Get Admin access
//     */
//    public boolean getAccess(String username,String password) {
//        boolean access = false;
//
//        if(username.equals("admin") && password.equals("m^3")) {
//            access = true;
//        }
//        return access;
//    }
//

    /**
     * Set a game by index after a change.
     */
    public void setLiveGames(int index, Game g) {
        liveGames[index] = g;
    }

    /*
     * Set the date of the last update to a game. Make sure that database and
     * live games are in sync.
     */

//    public void setGame(int index, Calendar value) {
//        games[index] = value;
//    }

    /*
     * Get date of last update of a game. This is used to either find out if
     * we have to access the database or not.
     */
//    public Date getGames(int index) {
//        return games[index];
//    }



    /**
     * Start a TCP connection using the port.
     * Create a thread and give it the client socket.
     */
    public void startTCP(int port) {
        try {
            tcp = new ServerSocket(port);
            System.out.println("TCP port open at "+port);

        }
        catch (Exception e){
            e.printStackTrace();
            System.exit(0);
        }

        while(running) {
            /*
             * if (updateFlag)
             * handleUDP
             *
             */
            try {
            Socket client = tcp.accept();
            Worker w = new Worker(this, client, getDb());
            w.start();

            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
        db.closeConnection();
    }

    /**
     * Start a UDP connection
     * Do we need UDP?
     */

    public void startUDP(int port) {
        try {
            udp = new DatagramSocket(port);
            System.out.println("UDP port open at "+port);

        }
        catch (Exception e){
            e.printStackTrace();
        }

    }

}
